﻿(function ($) {
    $.WebSetting = {
        recall: false,
        recall_times: 0,
        settingsUrl: location.href,
    };


    do {
        var index = $.WebSetting.settingsUrl.lastIndexOf("/");
        $.WebSetting.settingsUrl = $.WebSetting.settingsUrl.substr(0, index);
        var url = $.WebSetting.settingsUrl + "/websettings.json";

        $.ajax({
            url: url,
            async: false,
            dataType: "json",
            success: function (data) {
                $.WebSetting = data;
                $.WebSetting.recall = false;
            },
            complete: function (resp) {
                switch (resp.status) {
                    case 404:
                        $.WebSetting.recall = true;
                        break;
                }
            }
        });
        $.WebSetting.recall_times = $.WebSetting.recall_times + 1;
    } while ($.WebSetting.recall && $.WebSetting.recall_times < 10);

    $.IsNullOrEmpty = function (str) {
        return (str == null || str == "");
    }

    $.CombinURL = function (url1, url2) {
        if ($.IsNullOrEmpty(url1)) {
            return url2;
        } else {
            if ($.IsNullOrEmpty(url2)) {
                return url1;
            } else {
                var ch = "/", ch1 = url1.charAt(url1.length - 1), ch2 = url2.charAt(0);
                if (ch1 == ch && ch2 == ch) {
                    return url1 + url2.substr(1);
                } else if (ch1 != ch && ch2 != ch) {
                    return url1 + "/" + url2;
                } else {
                    return url1 + url2;
                }
            }
        }
    }

    $.UrlAddParameter = function (url, name, value) {
        if (!$.IsNullOrEmpty(url) && !$.IsNullOrEmpty(name)) {
            if (url.indexOf('?') > 0) {
                var p = url.indexOf(name);
                if (p < 0) {
                    url += "&" + name + "=" + value;
                } else {
                    var t = url.indexOf("=", p);
                    var k = url.indexOf("&", t);
                    url = url.substr(0, t + 1) + value + (k >= 0 ? url.substr(k, url.length - k) : "");
                }
            } else {
                url += "?" + name + "=" + value;
            }
        }

        return url;
    }

    $._appPath = $.CombinURL(location.href.substring(0, location.href.indexOf('/', location.href.indexOf('//') + 2)), $.WebSetting.AppPath);

    $.Content = function (url) {
        if (!$.IsNullOrEmpty(url)) {
            if (url.charAt(0) == '~') {
                var path = $._appPath;
                if ($.IsNullOrEmpty(path) || path == "/") {
                    url = url.substr(1);
                } else {
                    return $.CombinURL(path, url.substr(1));
                }
            } else if (url.charAt(0) == '{') {
                var n = url.indexOf('}') - 1;
                if (n > 0) {
                    var s = url.substr(1, n);
                    var path = eval("$." + url.substr(1, n));
                    if ($.IsNullOrEmpty(path) || path == "/") {
                        url = url.substr(n + 2);
                    } else {
                        return $.CombinURL(path, url.substr(n + 2));
                    }
                }
            }
        }
        return url;
    }

    $.fn.form_to_json = function (fun) {
        var j = {};
        var n = this.serializeArray();
        for (var i = 0; i < n.length; i++) {
            j[n[i].name] = n[i].value;
        }

        if (fun instanceof Function) {
            fun(this, j);
        }

        return j;
    }
})(jQuery);


(function ($) {
    if (JSON.parse) {
        var _JSON_PARSE = JSON.parse;
        JSON.parse = function (s) {
            try {
                return _JSON_PARSE(s);
            } catch (e) {
                try {
                    return eval(s);
                } catch (ee) {
                    return null;
                }
            }
        }
    }

    $.ajaxSettings.beforeSend = function (req) {
        if (!$.IsNullOrEmpty($.WebSetting.AppName)) {
            req.setRequestHeader("app", $.WebSetting.AppName);
        }
        if (this.url != undefined && this.url != null && this.url != "") {
            this.url = $.Content(this.url);
        }
    }

    $.ajaxSettings.complete = function (resp) {
        if (this.recall != true) {
            switch (resp.status) {
                case 401:
                    location.href = $.Content("~/Account/Login");
                    break;

                case 403:
                case 404:
                case 405:
                case 406:
                case 407:
                    if (this.need_recall == true) {
                        this.recall = true;
                        $.ajax(this);
                    }
                    break;
            }
        }
    }

    $.ajaxSettings.cache = false;

    if ($.Brower.Current.Type !== $.BrowerType.MSIE) {
        var _xhr = $.ajaxSettings.xhr;
        $.ajaxSettings.xhr = function () {
            var r = _xhr();
            if (r) {
                r.withCredentials = true;
            }
            return r;
        }
    }
})(jQuery);


(function ($) {
    function JSCache(second) {
        this.m_cache = new Array();
        this.m_time = new Date();
        this.m_expired_second = 30;
        if (parseInt(second) > 0) {
            this.m_expired_second = second;
        }

        this.Exist = function (key) {
            return this.m_cache[key] != undefined;
        }

        this.Put = function (key, v) {
            this.m_cache[key] = v;
        }

        this.Get = function (key, callback) {
            this.ClearExpired();
            var v = this.m_cache[key];
            if (!v && callback instanceof Function) {
                v = callback(key);
                this.m_cache[key] = v;
            }

            return v;
        }

        this.Get2 = function (key, value, callback) {
            this.ClearExpired();
            var v = this.m_cache[key];
            if (!v && callback instanceof Function) {
                v = callback(key, value);
                this.m_cache[key] = v;
            }

            return v;
        }

        this.Get3 = function (key, value, callback, force) {
            this.ClearExpired();
            if (force==true) {
                if (callback instanceof Function) {
                    var v = callback(key, value);
                    this.m_cache[key] = v;
                }
            } else {
                var v = this.m_cache[key];
                if (!v && callback instanceof Function) {
                    v = callback(key, value);
                    this.m_cache[key] = v;
                }
            }

            return v;
        }

        this.ClearAll = function () {
            this.m_cache = new Array();
        }

        this.ClearExpired = function () {
            if (((new Date() - this.m_time) / 1000) >= this.m_expired_second) {
                this.m_cache = new Array();
                this.m_time = new Date();
            }
        }
    }

    $._JC = [];

    $.JC = function (name) {
        if ($._JC[name] == null) {
            $._JC[name] = new JSCache();
        }

        return $._JC[name];
    }
})(jQuery);


function OnComboboxValueValid() {
    var nv = $(this).combobox('getValue');
    if (nv != undefined && nv != "") {
        var find = false;
        var values = $(this).combobox('getData');
        for (var i = 0; i < values.length; i++) {
            if (values[i][$(this).combobox("options").valueField] == nv) {
                find = true;
                break;
            }
        }
        if (find == false) {
            $(this).combobox('clear');
        }
    } else {
        $(this).combobox('clear');
    }
}

function OnCombogridValueValid() {
    var nv = $(this).combogrid("getValue");
    if (nv != undefined && nv != "") {
        var find = false;
        var values = $(this).combogrid("grid").datagrid('getData').rows;
        for (var i = 0; i < values.length; i++) {
            if (values[i][$(this).combogrid("options").idField] == nv) {
                find = true;
                break;
            }
        }
        if (find == false) {
            $(this).combogrid('clear');
            $(this).combogrid("grid").datagrid('reload', {});
        }
    } else {
        $(this).combogrid('clear');
        $(this).combogrid("grid").datagrid('reload', {});
    }
}


//function ToolBarOnInit() {
//    var panel = $(this);
//    var t = panel.panel("options").title;
//    if (!$.IsNullOrEmpty(t)) {
//        var ts = $(t);
//        if (ts.length > 0) {
//            var pt = panel.parent().find(".panel-title");
//            pt.html("");
//            ts.children().each(function () {
//                $(this).appendTo(pt);
//            });
//        }
//    }
//}

function $dlg(name) {
    var dlg = $("#" + name);
    if (dlg.length <= 0) {
        dlg = $("<div id='" + name + "' class='easyui-dialog' data-options='closed:true'></div>");
        dlg.appendTo($(document.body));
        $.parser.parse(dlg);
        return dlg;
    }
    return dlg;
}

function $$ (p, k) {
    return $(p).find(k);
};

